package www.lagou.app;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import www.lagou.entity.Employee;
import www.lagou.utils.DruidUtils;
import java.sql.SQLException;
import java.util.List;
public class EmployeeTest {
    /**
     * 需求1: 查询所有的员工信息 (不包含没有部门的员工)。
     */
    public static void queryTaskOne(){
        QueryRunner qr=new QueryRunner(DruidUtils.DATASOURCE);
        String sql="SELECT a.*,b.deptname FROM employee a INNER JOIN dept b ON a.did=b.id";
        try {
            List<Employee> employeeList = qr.query(sql, new BeanListHandler<Employee>(Employee.class));
            System.out.println("查询所有的员工信息 (不包含没有部门的员工)：");
            for (Employee employee : employeeList) {
                System.out.println(employee);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 需求2: 查询每个员工的 姓名, 薪资 和 所属部门名称
     */
    public static void queryTaskTwo(){
        QueryRunner qr=new QueryRunner(DruidUtils.DATASOURCE);
        String sql="SELECT a.NAME,a.salary,b.deptname FROM employee a INNER JOIN dept b ON a.did=b.id";
        try {
            List<Employee> employeeList = qr.query(sql, new BeanListHandler<Employee>(Employee.class));
            System.out.println("查询每个员工的 姓名, 薪资 和 所属部门名称：");
            for (Employee employee : employeeList) {
                System.out.println("姓名："+employee.getName());
                System.out.println("薪资："+employee.getSalary());
                System.out.println("部门："+employee.getDeptname());
                System.out.println("--------------------------------");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        queryTaskOne();
        System.out.println("------------------------------------------------------------------------------------------------------------------");
        queryTaskTwo();
    }
}
